package com.dy.course.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.dy.course.common.BaseException;
import com.dy.course.common.BaseService;
import com.dy.course.common.Constant;
import com.dy.course.entity.DepartmentQueryInfo;
import com.dy.course.model.Department;
import com.dy.course.service.IDepartmentService;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class DepartmenetService extends BaseService<Department> implements IDepartmentService {

	@Override
	public List<Department> getSubList(DepartmentQueryInfo query) {
		logger.info("【查询学校部门列表】,参数信息:" + JSON.toJSONString(query));
		Example example = new Example(Department.class);
		Criteria criteria = example.createCriteria();
		if(query.getLevel()!=null && query.getLevel() == 1){
			if(query.getName()!=null){
				criteria.andEqualTo("level", query.getLevel()).andCondition("(name like '%" +query.getName()+ "%')");
			}else{
				criteria.andEqualTo("level", query.getLevel());
			}
		}else if(query.getParentId()!= null){
			criteria.andEqualTo("parentId", query.getParentId());
		}else{
			throw new BaseException(Constant.Code.BAD_PARAMETER,"错误信息",null);
		}
		return getMapper().selectByExample(example);
	}
}
